var S_UNIT = ["", "K", "M", "B", "T", "P"];
export const Common = {
    isInArr(item, array) {
        for (let i = 0; i < array.length; i++) {
            if (item === array[i]) {
                return true;
            }
        }
        return false;
    },

    setGray(node) {
        if (!cc.isValid(node)) {
            return;
        }
        let sprite = node.getComponent(cc.Sprite);
        if (!sprite) {
            return;
        }
        sprite.setMaterial(0, cc.Material.getInstantiatedBuiltinMaterial('2d-gray-sprite'));
    },

    setNormal(node) {
        if (!cc.isValid(node)) {
            return;
        }
        let sprite = node.getComponent(cc.Sprite);
        if (!sprite) {
            return;
        }
        sprite.setMaterial(0, cc.Material.getInstantiatedBuiltinMaterial('2d-sprite'));
    },

    setGrayRecursively(node) {
        if (!cc.isValid(node)) {
            return;
        }
        this.setGray(node);
        if (node.children.length <= 0) {
            return;
        }
        for (let i = 0; i < node.children.length; i++) {
            this.setGrayRecursively(node.children[i]);
        }
    },
    setNormalRecursively(node) {
        if (!cc.isValid(node)) {
            return;
        }
        this.setNormal(node);
        if (node.children.length <= 0) {
            return;
        }
        for (let i = 0; i < node.children.length; i++) {
            this.setNormalRecursively(node.children[i]);
        }
    },

    isToday(str) {
        if (new Date(str).toDateString() === new Date().toDateString()) {
            //今天
            console.log("当天");
            return true;
        } else if (new Date(str) < new Date()) {
            //之前
            console.log("以前的日期");
            return false;
        }
    },

    loadAvatar(sprite, url, cb) {
        if (!cc.isValid(sprite)) {
            cb && cb(false);
            return;
        }

        cc.loader.load({ url: url, type: 'png' }, (err, res) => {
            if (err) {
                console.log(err);
                cb && cb(false);
                return;
            }
            if (!cc.isValid(sprite)) {
                cb && cb(false);
                return;
            }
            sprite.spriteFrame = new cc.SpriteFrame(res);
            cb && cb(true);
        })
    },

    setSpriteFrame(sprite, url, cb) {
        if (!cc.isValid(sprite)) {
            cb && cb(false);
            return;
        }
        cc.loader.loadRes(url, cc.SpriteFrame, (err, res) => {
            if (err) {
                console.log(err);
                cb && cb(false);
                return;
            }
            if (!cc.isValid(sprite)) {
                cb && cb(false);
                return;
            }
            sprite.spriteFrame = res;
            cb && cb(true);
        });
    },

    shakeEffect (node, duration) {
        let oldPos = node.position;
        node.runAction(
            cc.repeatForever(
                cc.sequence(
                    cc.moveTo(0.02, oldPos.add(cc.v2(5, 7))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(-6, 7))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(-13, 3))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(3, -6))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(-5, 5))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(2, -8))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(-8, -10))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(3, 10))),
                    cc.moveTo(0.02, oldPos.add(cc.v2(0, 0)))
                )
            )
        );

        setTimeout(() => {
            node.stopAllActions();
            node.position = oldPos;
        }, duration * 1000);
    },















    getTimest() {
        var tmp = Date.parse(new Date()).toString();
        tmp = tmp.substr(0, 10);
        return Number(tmp);
    },

    loadAvatar(spr, avatar, isRobot) {
        
    },
    
    // degree 最多显示的位数
    formatCoinStr: function (coin, degree) {
        coin = this.toBigNumber(coin);
        // coin = BigNumber(coin);
        // coin.sd(5);
        coin = coin.toFixed();
        let index = coin.indexOf('.');
        if (index != -1) {
            coin = coin.substring(0, index);
        }
        var level = Math.floor((coin.length - 1) / 3);
        var length = coin.length - level * 3;

        if (length <= degree - 3) {
            level--;
        }

        if (level < 0) {
            level = 0;
        }

        var unit;
        coin = coin.substring(0, coin.length - 3 * level);

        if (level < 6) {
            unit = S_UNIT[level];
        } else {
            let num1 = 97 + Math.floor((level - 6) / 26);
            let num2 = 97 + (level - 6) % 26;
            let unit1 = String.fromCharCode(num1);
            let unit2 = String.fromCharCode(num2);
            unit = unit1 + unit2;
        }
        // if (level > S_UNIT.length - 1) {
        //     unit = S_UNIT[S_UNIT.length - 1] + (level - (S_UNIT.length - 1));
        // }
        return String.format("{0}{1}", coin, unit);
    },

    toBigNumber(arg) {
        if (arg) {
            if (typeof (arg) == 'object') {
                return arg;
            }
            return math.bignumber(arg);
        } else {
            return math.bignumber('0');
        }
    },

    add(a, b) {
        // a = BigNumber(a)
        // return a.plus(b);
        a = this.toBigNumber(a);
        b = this.toBigNumber(b);
        return a.add(b);

    },

    sub(a, b) {
        // a = BigNumber(a)
        // return a.minus(b);

        a = this.toBigNumber(a);
        b = this.toBigNumber(b);
        return math.subtract(a, b);
    },
    mul(a, b) {
        // a = BigNumber(a)
        // return a.multipliedBy(b);

        a = this.toBigNumber(a);
        b = this.toBigNumber(b);
        return math.multiply(a, b);
    },
    div(a, b) {
        // a = BigNumber(a);
        // return a.dividedBy(b);

        a = this.toBigNumber(a);
        b = this.toBigNumber(b);
        return math.divide(a, b);
    },
    pow(a, b) {
        // a = BigNumber(a);
        // let c = a.pow(b);
        // return c;

        a = this.toBigNumber(a);
        b = this.toBigNumber(b);
        return a.pow(b);
    },
    compare(a, b) {
        // a = BigNumber(a)
        // return a.comparedTo(b);
        a = this.toBigNumber(a);
        b = this.toBigNumber(b);
        let res = math.compare(a, b);
        return res.toString();
    },
}